package at.bartinger.snake;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;


public class FingerSnake extends Activity{

	//Startmenu
	private ImageButton start;
	private ImageButton settings;
	private ImageButton info;
	private ImageButton quit;


	//Main Context
	private Context context;





	private Activity app;

	public static MediaPlayer aPlayer=new MediaPlayer();

	private boolean isInSettings = false;

	private OnClickListener playL;
	private OnClickListener infoL;
	private OnClickListener settingsL;
	private OnClickListener quitL;

	public static boolean Graphicends = false;
	public static int score = 0;
	
	private SettingsDialog sett;


	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		context=this;
		app=this;
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		//init Buttons
		start = (ImageButton) findViewById(R.id.start);
		info = (ImageButton) findViewById(R.id.info);
		settings = (ImageButton) findViewById(R.id.settings);
		quit = (ImageButton) findViewById(R.id.quit);
		sett = new SettingsDialog(this);
		

		//Play some Music
		aPlayer= MediaPlayer.create(this, R.raw.backgroundmusic);
		aPlayer.setLooping(true);
		aPlayer.start();

		playL = new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent rokongra = new Intent(context, GraphicActivity.class);
				startActivity(rokongra);

			}
		};

		settingsL = new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				sett.show();
			}
		};

		infoL = new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				dialog.setIcon(R.drawable.icon);
				dialog.setTitle("Spacey Snake"); 
			
				dialog.setMessage("Spacey Snake is my first game I developed.\n I'm sure that I will write furhter games for Android and I hope you download them too.\n You can read the source code at Google Code or read my Blog! \n Thanks to Marco H. for the background stuff");
				dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
					public void onClick(DialogInterface dialog, int whichButton) {}  
				});
				dialog.setNeutralButton("Google Code", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://code.google.com/p/spacey-snake/")));
					}

				});
				dialog.setNegativeButton("My Blog", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://bartinger.blogspot.com/")));
					}
				});
				dialog.show();

			}
		};

		quitL = new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					app.finish();
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		};

		setButtonMenuListener();


		
	}

	private void setButtonMenuListener(){
		//Set Listener to Buttons
		start.setOnClickListener(playL);

		settings.setOnClickListener(settingsL);

		info.setOnClickListener(infoL);

		quit.setOnClickListener(quitL);
	}


	@Override
	protected void onResume() {
		if(Graphicends){
		
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setIcon(R.drawable.icon);
			dialog.setTitle("Spacey Snake");  
			dialog.setMessage("Your score is " + score);
			dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {}  
			});
			dialog.create().show();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		aPlayer.stop();
		super.onDestroy();
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { 
			if(isInSettings){
				isInSettings= false;
				setContentView(R.layout.main);
				setButtonMenuListener();
				return false;
			}else return super.onKeyDown(keyCode, event); 



		}else return super.onKeyDown(keyCode, event); 

	} 



}

